﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageDevicetable.Queries
{
    public class GetDevicetableListByUserIdQueries : IRequest<Json<PaginatedList<GetDevicetableListByUserIdDTO>>>
    {
        /// <summary>
        /// 设备名称
        /// </summary>
        public string device { get; set; }
        public int pageIndex { get; set; }
        public int pageSize { get; set; }
    }

    public class GetDevicetableListByUserIdQueriesHandler : IRequestHandler<GetDevicetableListByUserIdQueries, Json<PaginatedList<GetDevicetableListByUserIdDTO>>>
    {
        private readonly IDevicetableRepository _idevocetableReporitory;
        private readonly ICurrentUserService _icurrentuserServices;
        public GetDevicetableListByUserIdQueriesHandler(IDevicetableRepository idevocetableReporitory, ICurrentUserService icurrentuserServices)
        {
            _idevocetableReporitory = idevocetableReporitory;
            _icurrentuserServices = icurrentuserServices;
        }
        public async Task<Json<PaginatedList<GetDevicetableListByUserIdDTO>>> Handle(GetDevicetableListByUserIdQueries request, CancellationToken cancellationToken)
        {
            int userid = _icurrentuserServices.GetUserId(); ;
#if DEBUG
            userid = 2;
#endif
            if (userid <= 0 || request.pageIndex <= 0 || request.pageSize <= 0)
            {
                return new Json<PaginatedList<GetDevicetableListByUserIdDTO>>() { Status = 1003, Detail = "参数不能为空", Data = new PaginatedList<GetDevicetableListByUserIdDTO>(new List<GetDevicetableListByUserIdDTO>() { }, 0, 0, 0) };

            }
            var model = await _idevocetableReporitory.GetDeviceListByUserId(userid,request.device, request.pageIndex, request.pageSize, cancellationToken);
            return new Json<PaginatedList<GetDevicetableListByUserIdDTO>>() { Status = 1000, Detail = "查询成功", Data = model };
        }
    }
}
